package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.model.VideoWithBLOBs;
import com.tarim.streetdance.service.VideoService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("/api/adminVideo")
public class AdminVideoApi {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private VideoService videoService;

    /**
     * 管理员 查询视频
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param title
     * @return
     */
    @RequestMapping(value = "/selectVideoByCondition")
    public JsonResult selectVideoByCondition (HttpServletRequest request,
                                              @RequestParam(value = "pageIndex", defaultValue = "0")Integer pageIndex,
                                              @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                                              @RequestParam(value = "title", defaultValue = "")String title,
                                              @RequestParam(value = "status", defaultValue = "0")String status) {
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后查看数据");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            PageBean<VideoWithBLOBs> pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("title", title);
            criteria.addExtField("status", status);
            pageBean = videoService.selectVideoByCondition(pageBean, criteria);
            js = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return js;
    }

    /**
     * 管理员 修改视频
     * @param request
     * @param video
     * @return
     */
    @RequestMapping(value = "/updateVideo", method = RequestMethod.POST)
    public JsonResult update (HttpServletRequest request, VideoWithBLOBs video) {
        JsonResult js;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                js = new JsonResult(Constants.STATUS_FAIL, "登录后查看数据");
                return js;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            if (user.getUserType() != 0) {
                js = new JsonResult(Constants.STATUS_FAIL, "非管理员用户没有权限访问");
                return js;
            }
            int i = videoService.updateByPrimaryKeySelective(video);
            if (i != 0) {
                js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
            }else {
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "系统异常");
        }
        return js;
    }
}
